<?php
/** Zend_Controller_Action */

class IndexController extends Zend_Controller_Action
{
	public function registerAction()
	{
		
		$validate_Alnum_Length = new Zend_Validate();
		$validate_Alnum_Length->addValidator(new Zend_Validate_StringLength(4, 16))
		               ->addValidator(new Zend_Validate_Alnum());
		               
		$validate_Length = new Zend_Validate();
		$validate_Length->addValidator(new Zend_Validate_StringLength(4, 16));
		
		$errors = array();
		
		// Validate the username
		if (!$validate_Alnum_Length->isValid($_REQUEST['username'])) 
		{
			$errors['username'] = array();
			foreach ($validate_Alnum_Length->getMessages() as $message) 
			{
		        $errors['username'][] = "$message";
		    }    
		}
		
		// Validate the password
		if (!$validate_Length->isValid($_REQUEST['password'])) 
		{
			$errors['password'] = array();
			foreach ($validate_Length->getMessages() as $message) 
			{
		        $errors['password'][] = "$message";
		    }    
		}
		if ($_REQUEST['password'] != $_REQUEST['password_ext'])
		{
			$errors['password'] = array();
			$errors['password'][] = "Passwords do not match";
		}
		
		$this->view->json = Zend_Json::encode($errors);
	}
	public function loginAction()
	{
		$session = new Zend_Session_Namespace('User');
		
    	$config = Zend_Registry::get('config');
		
		$dbAdapter = Zend_Db::factory($config->database->type, array(
		    'host'     => $config->database->host,
		    'username' => $config->database->username,
		    'password' => $config->database->password,
		    'dbname'   => $config->database->name
		));
		
		$authAdapter = new Zend_Auth_Adapter_DbTable($dbAdapter);
		$authAdapter->setTableName('adminstrators')
		            ->setIdentityColumn('admin_username')
		            ->setCredentialColumn('admin_password');
		$authAdapter->setIdentity($_REQUEST['username'])
            		->setCredential($_REQUEST['password']);
        $result = $authAdapter->authenticate();
          
        if ($result->isValid()) {
			
			Zend_Session::rememberMe(60*60*24*14);
			
			$data = $authAdapter->getResultRowObject();
			
			$session = new Zend_Session_Namespace('User');
			$session->company_id = $data->company_id;
			$session->setExpirationSeconds(60*60*24*14);
        	
        	unset($session->error);
			$this->_redirect('/home/company/edit');
        }
        else
        {
        	$session->error = '';
		    foreach ($result->getMessages() as $message)
		    {
		        $session->error .= $message;
		    }
			$this->_redirect('/');
        }
        
	}
	public function logoutAction()
	{
		Zend_Session::destroy($remove_cookie = true, $readonly = true);
		$this->_redirect('/index/index');
	}
	
	public function paypalAction()
	{
		require_once 'gateways/paypal/ipn.php';
		$session = new Zend_Session_Namespace('User');
		
		$ipn = new paypal_class();
		
		$ipn->add_field('business', 'sutabi_1185755230_biz@gmail.com');
		$ipn->add_field('item_name', 'Payment for Invoice #0001');
		
		$ipn->add_field('notify_url', 'http://shabb.com/gateways/paypal/ipn.process.php');
		$ipn->add_field('amount', '0.01');
		//$ipn->add_field('item_number', '1'); 
		$ipn->add_field('invoice', '000001');
		$ipn->add_field('on0', 'Company ID'); 
		$ipn->add_field('os0', '1');//Company ID
		$ipn->add_field('on1', 'Payment Type'); 
		$ipn->add_field('os1', 'Client:3');
		//$ipn->add_field('os1', 'Invoice:Staff|000001:2');
		$ipn->add_field('currency_code', 'USD');
		
		$this->view->paypal = $ipn;
		
	}
    public function indexAction()
    {
        $registry = new Zend_Controller_Request_Http();
		$session = new Zend_Session_Namespace('User');
		$front = Zend_Controller_Front::getInstance();
		
    	if($session->error){
    		$this->view->error = new SwatMessageDisplay('notice_message');
    		$this->view->error->add( 
	    		new SwatMessage($session->error,
	      		SwatMessage::ERROR), 1);
	      	
        	unset($session->error);
    	}
    	
    	$nav = new SwatNavBar();
    	$nav->title = 'Navigation Trail';
    	$nav->addEntry(new SwatNavBarEntry('Shabb', '#'));
    	$nav->addEntry(new SwatNavBarEntry('Login'));
    	$this->view->nav = $nav;
      	
    	$frame = new SwatFrame();
    	$frame->title = 'Shabb Project Management';
    	$form = new SwatForm('form_login');
    	$form->action = $registry->getBaseUrl().'/index/login';
    	
		$input = new SwatEntry('username');
    	$input->size = 24;
    	$input->value = $front->getParam('username');
    	$form->addWithField($input, 'Username');
    	
		$input = new SwatPasswordEntry('password');
    	$input->size = 24;
    	$input->value = $front->getParam('password');
    	$form->addWithField($input, 'Password');

    	$footer = new SwatFooterFormField();
    	$submit = new SwatButton();
    	$submit->title = 'Login';
    	
    	$footer->add($submit);
    	$form->add($footer);
    	$frame->add($form);
    	
    	$this->view->form = $frame;
    	
    	
    	$this->view->baseUrl = $registry->getBaseUrl();
    }
    
}

?>
